package com.tboss.template.sample.dwr.treeviewService;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

import com.tboss.template.sample.dwr.treeviewDao.TreeviewDao;
import com.tboss.template.sample.dwr.treeviewDto.TreeviewDto;
import com.tboss.global.utils.useful.SecurityUtils;

public class TreeviewJsonDaoService implements TreeviewService {
	
	private TreeviewDao treeviewDao;

	@Override
	public StringBuffer build(HashMap<String, String> opts) {
		return null;
	}

	@Override
	public StringBuffer build(HashMap<String, String> opts, TreeviewDao treeviewDao) {		
		StringBuffer rtnValue = new StringBuffer();
		
		this.treeviewDao = treeviewDao;
		
		TreeviewDto treeviewDto = new TreeviewDto();
		treeviewDto.setFnId(opts.get("rootTreeId"));
		treeviewDto.setFnOwner(SecurityUtils.getCurrentUserName());
		List<TreeviewDto> list = this.treeviewDao.getLeftMenu(treeviewDto);
		
		try {
			Map<String, List<TreeviewDto>> menu = new HashMap<String, List<TreeviewDto>>();
			menu.put(opts.get("jsonRootKey"), list);
			
			rtnValue.append(new ObjectMapper().writeValueAsString(menu));
			
		} catch (JsonGenerationException e) { e.printStackTrace();
		} catch (JsonMappingException e) { e.printStackTrace();
		} catch (IOException e) { e.printStackTrace();
		} catch (Exception e) { e.printStackTrace();
		}
		
		return rtnValue;
	}

}
